/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.DevicesManager;
import control.RoomManager;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Device;
import model.DeviceInRoom;

/**
 *
 * @author My PC
 */
public class PanelDeviceInRoom extends javax.swing.JPanel {

    /**
     * Creates new form PanelDeviceInRoom
     */
    DefaultListModel listDeviceModel = new DefaultListModel();
    DefaultTableModel tblModel = new DefaultTableModel();
    List<DeviceInRoom> listCopy = new ArrayList<DeviceInRoom>();

    public PanelDeviceInRoom() {
        initComponents();
        initControl();
        loadListBoxDevice();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstDevices = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtaDescription = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbStatus = new javax.swing.JCheckBox();
        spnQuantity = new javax.swing.JSpinner();
        btnAdd = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtRoomID = new javax.swing.JTextField();
        btnView = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tblDeviceInRoom = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        lblRoomID = new javax.swing.JLabel();
        btnDelete = new javax.swing.JButton();
        btnClearAll = new javax.swing.JButton();
        btnCopy = new javax.swing.JButton();
        btnPaste = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable1);

        jScrollPane4.setViewportView(jEditorPane1);

        lstDevices.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstDevices.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstDevicesMouseClicked(evt);
            }
        });
        lstDevices.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                lstDevicesKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(lstDevices);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("List devices");

        txtaDescription.setColumns(20);
        txtaDescription.setRows(5);
        jScrollPane2.setViewportView(txtaDescription);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Description");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Quantity");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Status");

        cbStatus.setSelected(true);
        cbStatus.setText("Useable");

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add-icon.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Times New Roman", 2, 36)); // NOI18N
        jLabel5.setForeground(java.awt.Color.blue);
        jLabel5.setText("Room ID");

        txtRoomID.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        txtRoomID.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtRoomIDKeyPressed(evt);
            }
        });

        btnView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Data-View-Details-icon.png"))); // NOI18N
        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Devices in room", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 2, 18))); // NOI18N

        tblDeviceInRoom.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane5.setViewportView(tblDeviceInRoom);

        jLabel6.setText("Room ID");

        lblRoomID.setForeground(java.awt.Color.blue);
        lblRoomID.setText("lblID");

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnClearAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1414395970_edit-clear-16.png"))); // NOI18N
        btnClearAll.setText("Clear all");
        btnClearAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearAllActionPerformed(evt);
            }
        });

        btnCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/copy.png"))); // NOI18N
        btnCopy.setText("Copy");
        btnCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopyActionPerformed(evt);
            }
        });

        btnPaste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1414395895_Paste.png"))); // NOI18N
        btnPaste.setText("Paste");
        btnPaste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPasteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRoomID)
                        .addGap(143, 143, 143)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addComponent(btnClearAll)
                        .addGap(18, 18, 18)
                        .addComponent(btnCopy)
                        .addGap(36, 36, 36)
                        .addComponent(btnPaste)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(lblRoomID)
                    .addComponent(btnDelete)
                    .addComponent(btnClearAll)
                    .addComponent(btnCopy)
                    .addComponent(btnPaste))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel3))
                                        .addGap(30, 30, 30)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(cbStatus)
                                            .addComponent(spnQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnAdd)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(txtRoomID, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnView))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cbStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnReset)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addComponent(txtRoomID, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnView))
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel3)
                                        .addComponent(spnQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jButton1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        if (lstDevices.getSelectedIndices().length < 1) {
            JOptionPane.showMessageDialog(null, "Select device before add");
            return;
        }
        if (isValidRoomDevice()) {
            Device d = (Device) lstDevices.getSelectedValue();
            DeviceInRoom dir = new DeviceInRoom(txtRoomID.getText(), d.getDeviceID(), d.getDeviceName(), (int) spnQuantity.getValue(), cbStatus.isSelected());
            if (RoomManager.insertDeviceToRoom(dir)) {
                JOptionPane.showMessageDialog(null, "Insert device ok");
                loadTable(txtRoomID.getText());
            } else {
                JOptionPane.showMessageDialog(null, "Insert device failed");
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void lstDevicesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstDevicesMouseClicked
        if (lstDevices.getSelectedIndices().length < 1) {
            return;
        }
        Device d = (Device) lstDevices.getSelectedValue();
        txtaDescription.setText(d.getDescription());
    }//GEN-LAST:event_lstDevicesMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        loadListBoxDevice();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void lstDevicesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstDevicesKeyReleased
        if ((evt.getKeyCode() == KeyEvent.VK_UP) || (evt.getKeyCode() == KeyEvent.VK_DOWN)) {
            if (lstDevices.getSelectedIndices().length < 1) {
                return;
            }
            Device d = (Device) lstDevices.getSelectedValue();
            txtaDescription.setText(d.getDescription());
        }
    }//GEN-LAST:event_lstDevicesKeyReleased

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        if (txtRoomID.getText() == null || txtRoomID.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "RoomID is null or empty");
            return;
        }
        loadTable(txtRoomID.getText());
    }//GEN-LAST:event_btnViewActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        lstDevices.clearSelection();
        txtaDescription.setText("");
        spnQuantity.setValue(0);
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCopyActionPerformed
        listCopy = new ArrayList<DeviceInRoom>();
        listCopy = RoomManager.getDeviceInRoomByID(lblRoomID.getText());
    }//GEN-LAST:event_btnCopyActionPerformed

    private void btnPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPasteActionPerformed
        for (DeviceInRoom dir : listCopy) {
            dir.setRoomID(lblRoomID.getText());
            if(!RoomManager.insertDeviceToRoom(dir)){
                JOptionPane.showMessageDialog(null, "Paste failed");
                break;
            }
            loadTable(txtRoomID.getText());
        }
    }//GEN-LAST:event_btnPasteActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if (tblDeviceInRoom.getSelectedRows().length < 1) {
            JOptionPane.showMessageDialog(null, "Select row before delete");
            return;
        }
        int id = (int) tblDeviceInRoom.getModel().getValueAt(tblDeviceInRoom.getSelectedRow(), 0);
        if (JOptionPane.showConfirmDialog(null, "Are u sure?") == JOptionPane.YES_OPTION) {
            if (RoomManager.deleteDeviceFromRoom(id)) {
                JOptionPane.showMessageDialog(null, "Delete ok");
                loadTable(lblRoomID.getText());
            } else {
                JOptionPane.showMessageDialog(null, "Delete failed");
            }
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    private void txtRoomIDKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRoomIDKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            btnViewActionPerformed(null);
        }
    }//GEN-LAST:event_txtRoomIDKeyPressed

    private void btnClearAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearAllActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Are u sure?") == JOptionPane.YES_OPTION) {
            List<DeviceInRoom> list = new ArrayList<>();
            list = RoomManager.getDeviceInRoomByID(lblRoomID.getText());
            for (DeviceInRoom dir : list) {
                if (RoomManager.deleteDeviceFromRoom(dir.getDeviceInRoomID())) {
                    loadTable(lblRoomID.getText());
                } else {
                    JOptionPane.showMessageDialog(null, "Clear failed");
                    break;
                }
            }
            JOptionPane.showMessageDialog(null, "Clear ok");

        }
    }//GEN-LAST:event_btnClearAllActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnClearAll;
    private javax.swing.JButton btnCopy;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnPaste;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnView;
    private javax.swing.JCheckBox cbStatus;
    private javax.swing.JButton jButton1;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblRoomID;
    private javax.swing.JList lstDevices;
    private javax.swing.JSpinner spnQuantity;
    private javax.swing.JTable tblDeviceInRoom;
    private javax.swing.JTextField txtRoomID;
    private javax.swing.JTextArea txtaDescription;
    // End of variables declaration//GEN-END:variables

    private void loadListBoxDevice() {
        List<Device> listD = new ArrayList<Device>();
        listD = DevicesManager.getAllDevices();
        listDeviceModel = new DefaultListModel();
        for (Device d : listD) {
            listDeviceModel.addElement(d);
        }
        lstDevices.setModel(listDeviceModel);
    }

    private void initControl() {
        txtaDescription.setEditable(false);
    }

    private void loadTable(String id) {
        tblModel = new DefaultTableModel();
        tblModel.addColumn("ID");
        tblModel.addColumn("DeviceID");
        tblModel.addColumn("Device");
        tblModel.addColumn("Quantity");
        tblModel.addColumn("Status");
        List<DeviceInRoom> list = new ArrayList<>();
        list = RoomManager.getDeviceInRoomByID(id);
        for (DeviceInRoom dir : list) {
            tblModel.addRow(RoomManager.convertDeviceInRoomToVector(dir));
        }
        tblDeviceInRoom.setModel(tblModel);
        lblRoomID.setText(id);
    }

    private boolean isValidRoomDevice() {
        try {
            Integer.parseInt(spnQuantity.getValue().toString());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Invalid quantity");
            return false;
        }
        if (null == txtRoomID.getText() || txtRoomID.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Invalid RoomID");
            return false;
        }
        return true;
    }
}
